package com.aaron.kjviewutils.footballleague;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecyclerDividerForOther;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Aaron on 2017/2/20 10:35
 * 邮箱：kaijin.fang@edianniu.com
 */
public class LleagueFragment extends Fragment {

    @Bind(R.id.lleagueRecyclerView)
    RecyclerView lleagueRecyclerView;

    private FootBallLleagueActivity rootAty;
    private View rootView;
    private LleagueAdapter adapter;
    private List<FootBallLleagueBean.Lleagues.Lleague.Team> datas;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (FootBallLleagueActivity) getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_lleague, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecycler();
    }

    private void initRecycler()
    {
        datas = new ArrayList<FootBallLleagueBean.Lleagues.Lleague.Team>();
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            int type = bundle.getInt("index");
            switch (type)
            {
                case 0:
                {
                    //德甲2015
                    FootBallLleagueBean lleagues = rootAty.data;
                    if(null!=lleagues)
                    {
                        if(null!=lleagues.result)
                        {
                            List<FootBallLleagueBean.Lleagues.Lleague> lleague = lleagues.result.djTypeCns;
                            if(null!=lleague&&!lleague.isEmpty())
                            {
                                for(FootBallLleagueBean.Lleagues.Lleague inner:lleague)
                                {
                                    if("2015".equals(inner.season))
                                    {
                                        if(null!=inner.teamInfoSet&&!inner.teamInfoSet.isEmpty())
                                        {
                                            datas = inner.teamInfoSet;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
                case 1:
                {
                    //法甲2015
                    FootBallLleagueBean lleagues = rootAty.data;
                    if(null!=lleagues)
                    {
                        if(null!=lleagues.result)
                        {
                            List<FootBallLleagueBean.Lleagues.Lleague> lleague = lleagues.result.fjTypeCns;
                            if(null!=lleague&&!lleague.isEmpty())
                            {
                                for(FootBallLleagueBean.Lleagues.Lleague inner:lleague)
                                {
                                    if("2015".equals(inner.season))
                                    {
                                        if(null!=inner.teamInfoSet&&!inner.teamInfoSet.isEmpty())
                                        {
                                            datas = inner.teamInfoSet;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
                case 2:
                {
                    //西甲2015
                    FootBallLleagueBean lleagues = rootAty.data;
                    if(null!=lleagues)
                    {
                        if(null!=lleagues.result)
                        {
                            List<FootBallLleagueBean.Lleagues.Lleague> lleague = lleagues.result.xjTypeCns;
                            if(null!=lleague&&!lleague.isEmpty())
                            {
                                for(FootBallLleagueBean.Lleagues.Lleague inner:lleague)
                                {
                                    if("2015".equals(inner.season))
                                    {
                                        if(null!=inner.teamInfoSet&&!inner.teamInfoSet.isEmpty())
                                        {
                                            datas = inner.teamInfoSet;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
                case 3:
                {
                    //英超2015
                    FootBallLleagueBean lleagues = rootAty.data;
                    if(null!=lleagues)
                    {
                        if(null!=lleagues.result)
                        {
                            List<FootBallLleagueBean.Lleagues.Lleague> lleague = lleagues.result.ycTypeCns;
                            if(null!=lleague&&!lleague.isEmpty())
                            {
                                for(FootBallLleagueBean.Lleagues.Lleague inner:lleague)
                                {
                                    if("2015".equals(inner.season))
                                    {
                                        if(null!=inner.teamInfoSet&&!inner.teamInfoSet.isEmpty())
                                        {
                                            datas = inner.teamInfoSet;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
                case 4:
                {
                    //意甲2015
                    FootBallLleagueBean lleagues = rootAty.data;
                    if(null!=lleagues)
                    {
                        if(null!=lleagues.result)
                        {
                            List<FootBallLleagueBean.Lleagues.Lleague> lleague = lleagues.result.yjTypeCns;
                            if(null!=lleague&&!lleague.isEmpty())
                            {
                                for(FootBallLleagueBean.Lleagues.Lleague inner:lleague)
                                {
                                    if("2015".equals(inner.season))
                                    {
                                        if(null!=inner.teamInfoSet&&!inner.teamInfoSet.isEmpty())
                                        {
                                            datas = inner.teamInfoSet;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
                case 5:
                {
                    //中超2015
                    FootBallLleagueBean lleagues = rootAty.data;
                    if(null!=lleagues)
                    {
                        if(null!=lleagues.result)
                        {
                            List<FootBallLleagueBean.Lleagues.Lleague> lleague = lleagues.result.zcTypeCns;
                            if(null!=lleague&&!lleague.isEmpty())
                            {
                                for(FootBallLleagueBean.Lleagues.Lleague inner:lleague)
                                {
                                    if("2015".equals(inner.season))
                                    {
                                        if(null!=inner.teamInfoSet&&!inner.teamInfoSet.isEmpty())
                                        {
                                            datas = inner.teamInfoSet;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
                default:
                    break;
            }
            lleagueRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                    StaggeredGridLayoutManager.VERTICAL));
            adapter = new LleagueAdapter(rootAty, datas);
            lleagueRecyclerView.setAdapter(adapter);
            lleagueRecyclerView.setItemAnimator(new DefaultItemAnimator()); // 默认动画
            lleagueRecyclerView.addItemDecoration(new RecyclerDividerForOther(rootAty, 3));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
